package com.eee.machine.concurrent;

import java.util.concurrent.*;

public class CustomThreadPool {

    /**
     *  可缓存的线程池，新建线程执行任务后如果又新任务进来则老的可用线程将被新的任务触发重新执行
     *  如果线程超过60秒内没执行，那么将被终止并从池中删除
     */
    public static final ExecutorService cachedThreadPool = Executors.newCachedThreadPool();
    /**
     * 定长的线程池，如果没有任务执行，那么线程会一直等待
     * 这里固定有10个核心线程，当线程没满10个的时候会新建线程，如果超过10个则在任务队列中等待前面线程执行完
     * 采用无界的阻塞队列LinkedBlockingQueue
     */
    public static final ExecutorService fixedThreadPool = Executors.newFixedThreadPool(10);

    public static int corePoolSize = 10; //核心线程池大小
    public static int maximumPoolSize = Integer.MAX_VALUE; //最大允许的线程数
    public static long keepAliveTime = 60; //线程允许的空闲时间，超时干掉
    public static TimeUnit unit = TimeUnit.SECONDS; //空闲时间的单位
    //任务队列
    public static BlockingQueue<Runnable> workQueue = new LinkedBlockingQueue<>();

    public static ThreadPoolExecutor createThreadPoolExecutor(){

        return new ThreadPoolExecutor(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue);
    }
}
